import { effect, track, trigger, ITERATE_KEY } from "./index.js";

// 5.3 如何代理object

let user = {
  name: null,
  age: 29,
};
// const ITERATE_KEY = Symbol();
let userProxy = new Proxy(user, {
  get(target, key, receiver) {
    track(target, key);
    return Reflect.get(target, key, receiver);
  },
  set(target, key, val, receiver) {
    // 如果属性不存在，则说明是在添加新属性，否则是设置已有属性
    const type = Object.prototype.hasOwnProperty.call(target, key)
      ? "SET"
      : "ADD";
    const res = Reflect.set(target, key, val, receiver);
    trigger(target, key, type);
    return res;
  },
  has(target, key) {
    return Reflect.has(target, key);
  },
  ownKeys(target, key) {
    track(target, ITERATE_KEY);
    return Reflect.ownKeys(target);
  },
});
// console.log("has==", "name" in userProxy);

effect(() => {
  for (let key in userProxy) {
    console.log(key);
  }
});
setTimeout(() => {
  userProxy.age = 28;
  //   userProxy.phone = "18363";
}, 1500);
